package wayden;

import java.io.File;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Random;

import javax.servlet.ServletContext;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.util.ServletContextAware;

import training.db.Connect;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class FileUploadAction extends ActionSupport implements
		ServletContextAware {

	private File userImage;

	private String userImageContentType;

	private String userImageFileName;
	private ServletContext context;
	public static final String UPDATE_PROFILE = "UPDATE profile "
	    + "SET logo = ? WHERE id = ?";
	
	Map session;

	public void setServletContext(ServletContext context) {
		this.context = context;
	}

	public String execute() {
		session = ActionContext.getContext().getSession();
		Connection conn = null;
		PreparedStatement ps;
		if(session.get("user") != null){
			System.err.println(getUserImageFileName());
			System.err.println(getUserImageContentType());
			try {
				String targetDirectory = context.getRealPath("/upload");
				System.err.println("__" + targetDirectory);
				System.err.println("__" + generateFileName(userImageFileName));
				String targetFileName = generateFileName(userImageFileName);
				// String targetDir = ServletActionContext.getServletContext()
				// .getRealPath("/upload");
				String targetDir = "/";
				System.err.println(targetDir);
				File target = new File(targetDirectory, targetFileName);
				FileUtils.copyFile(userImage, target);
				System.out.println(userImageFileName);
				conn = Connect.getConnection();
				ps = conn.prepareStatement(UPDATE_PROFILE);
				ps.setString(1, targetFileName);
				ps.setString(2, session.get("user").toString());
				ps.executeUpdate();
			} catch (Exception e) {
				e.printStackTrace();
			}
			return SUCCESS;
		}else return "error";
		
	}

	public File getUserImage() {
		return userImage;
	}

	public void setUserImage(File userImage) {
		this.userImage = userImage;
	}

	public String getUserImageContentType() {
		return userImageContentType;
	}

	public void setUserImageContentType(String userImageContentType) {
		this.userImageContentType = userImageContentType;
	}

	public String getUserImageFileName() {
		return userImageFileName;
	}

	public void setUserImageFileName(String userImageFileName) {
		this.userImageFileName = userImageFileName;
	}

	private String generateFileName(String fileName) {
		DateFormat format = new SimpleDateFormat("yyMMddHHmmss");
		String formatDate = format.format(new Date());

		int random = new Random().nextInt(10000);

		int position = fileName.lastIndexOf(".");
		String extension = fileName.substring(position);

		return formatDate + random + extension;
	}
}